package com.china08.yxyapi.service.mg.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.util.JpushForMessageUtil;
import com.china08.yxyapi.util.JpushForNotificationUtil;

/**
 * 学生报告线程
 *
 */
public class ExamStuReportRunnable extends Thread {

    private static final Logger LOGGER = Logger.getLogger(ExamStuReportRunnable.class);

    private String yxyApiYxUrl;

    private String schoolId;

    private List<String> stuLst;

    private RestTemplate restTemplate;

    public ExamStuReportRunnable(List<String> stuLst, String schoolId, String yxyApiYxUrl, RestTemplate restTemplate) {
        this.stuLst = stuLst;
        this.schoolId = schoolId;
        this.yxyApiYxUrl = yxyApiYxUrl;
        this.restTemplate = restTemplate;
    }

    @Override
    public void run() {
        try {
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
            Map<String, Object> formMap = new HashMap<String, Object>();
            formMap.put("studentIds", stuLst);
            HttpEntity<Map<String, Object>> formEntity = new HttpEntity<Map<String, Object>>(formMap, headers);
            StudentParentRespModel httpResult = restTemplate.postForObject(yxyApiYxUrl + "/comm/par/find/stu/parent",
                    formEntity, StudentParentRespModel.class);

            List<StudentParent> studentParents = httpResult.studentParents;
            List<String> perLst = new ArrayList<String>();
            for (StudentParent studentParent : studentParents) {
                perLst.add(studentParent.parentId);

                List<String> stuPerLst = new ArrayList<String>();
                stuPerLst.add(studentParent.parentId);
                // 发送通知
                Map<String, String> mapNotification = new HashMap<String, String>();
                mapNotification.put("modelId", "chengjidan");
                List<String> tabAndList1 = new ArrayList<>();
                tabAndList1.add("PARENTS" + schoolId);
                JpushForNotificationUtil.sendNotification("您的孩子（" + studentParent.studentName + "）有一份新的成绩单", "成绩单",
                        mapNotification, stuPerLst, tabAndList1);
            }

            // 发送消息
            List<Integer> roleIdList = new ArrayList<>();
            roleIdList.add(1);
            JpushForMessageUtil.sendMessage(9, roleIdList, schoolId, perLst, null);

        } catch (Exception e) {
            LOGGER.info("获取学生家长列表接口错误：" + e.getMessage());
        }
    }

    protected static class StudentParentRespModel {
        public List<StudentParent> studentParents;
    }

    protected static class StudentParent {
        public String studentId;
        public String studentName;
        public String parentId;
    }

}
